package com.tutk.P2PCam264;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import java.util.List;

public class PhotoViewerActivity  extends Activity implements GestureDetector.OnGestureListener, View.OnTouchListener
{

    private static final int FLING_MIN_DISTANCE = 20;
    private static final int FLING_MIN_VELOCITY = 0;

    private GestureDetector mGestureDetector;

	private String mFileName;

    private List<String> IMAGE_FILES;
    private int mPosition;
    private int mSize;
    private ImageView iv;
    private Bitmap bm;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    	
        super.onCreate(savedInstanceState);
        System.gc();
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        //BitmapFactory.Options bfo = new BitmapFactory.Options();
        //bfo.inSampleSize = 2;
        IMAGE_FILES = extras.getStringArrayList("files");
        mFileName = extras.getString("filename");
        mPosition = extras.getInt("pos");
        mSize = IMAGE_FILES.size();

        mGestureDetector = new GestureDetector(this, this);

        iv = new ImageView(getApplicationContext());
        iv.setOnTouchListener(this);
        Bitmap bm = BitmapFactory.decodeFile(mFileName);// ,bfo);
        iv.setImageBitmap(bm);
        setContentView(iv);
    }

    @Override
    public boolean onTouch (View v, MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        return true;
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // Fling left
            if (mPosition == (mSize - 1)) {
                mPosition = 0;
                mFileName = IMAGE_FILES.get(mPosition);
                bm = BitmapFactory.decodeFile(mFileName);
                iv.setImageBitmap(bm);
                setContentView(iv);
            } else {
                mPosition += 1;
                mFileName = IMAGE_FILES.get(mPosition);
                bm = BitmapFactory.decodeFile(mFileName);
                iv.setImageBitmap(bm);
                setContentView(iv);
            }
//            tvTittle.setText((mPosition + 1) + " of " + mSize);
        } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // Fling right
            if (mPosition == 0) {
                mPosition = mSize - 1;
                mFileName = IMAGE_FILES.get(mPosition);
                bm = BitmapFactory.decodeFile(mFileName);
                iv.setImageBitmap(bm);
                setContentView(iv);
            } else {
                mPosition -= 1;
                mFileName = IMAGE_FILES.get(mPosition);
                bm = BitmapFactory.decodeFile(mFileName);
                iv.setImageBitmap(bm);
                setContentView(iv);
            }
//            tvTittle.setText((mPosition + 1) + " of " + mSize);
        }
        return false;
    }
}